package com.hua.store.dao.impl;

import com.hua.store.dao.UserDao;
import com.hua.store.domin.User;
import com.hua.store.utils.JDBCUtils;
import jdk.nashorn.internal.scripts.JD;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    /**
     * 这是用户注册的方法
     *
     * @param user
     * @throws SQLException
     */
    @Override
    public void userRegister(User user) throws SQLException {
        String sql = "insert into user values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
        queryRunner.update(sql,
                user.getUid(),
                user.getUsername(),
                user.getPassword(),
                user.getName(),
                user.getEmail(),
                user.getTelephone(),
                user.getBirthday(),
                user.getSex(),
                user.getState(),
                user.getCode()
        );
    }

    /**
     * 这是用户激活的方法
     *
     * @param code
     * @return User
     */
    @Override
    public User userActive(String code) {
        try {
            String sql = "select * from user where code=?";
            QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
            User user = queryRunner.query(sql, new BeanHandler<User>(User.class), code);
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 用户激活之后更新数据库中用户的状态state和清除激活码
     *
     * @param user
     */
    @Override
    public void userActiveLate(User user) {
        try {
            String sql = "update user set username=?, password=?, name=?, email=?, telephone=?, birthday=?, sex=?, state=?, code=? where uid = ?";
            Object[] params = {user.getUsername(),
                    user.getPassword(),
                    user.getName(),
                    user.getEmail(),
                    user.getTelephone(),
                    user.getBirthday(),
                    user.getSex(),
                    user.getState(),
                    user.getCode(),
                    user.getUid()
            };
            QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
            queryRunner.update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 这是用户登录Dao
     *
     * @param user
     * @return
     * @throws SQLException
     */
    @Override
    public User userLogin(User user) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
        String sql = "select * from user where username = ? and password =?";
        return queryRunner.query(sql, new BeanHandler<User>(User.class), user.getUsername(), user.getPassword());
    }

    @Override
    public User checkUser(String username) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
        return queryRunner.query("select * from user where username = ?", new BeanHandler<User>(User.class), username);
    }
}
